package com.youi.service.impl;

import com.youi.common.DelStatus;
import com.youi.dao.UserSearchHistoryDao;
import com.youi.entity.UserSearchHistory;
import com.youi.service.UserSearchHistoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserSearchHistoryServiceImpl extends BaseServiceImpl<UserSearchHistory> implements UserSearchHistoryService {

    private UserSearchHistoryDao userSearchHistoryDao;

    @Autowired
    public void setfaultRecordDao(UserSearchHistoryDao userSearchHistoryDao) {
        this.userSearchHistoryDao = userSearchHistoryDao;
        super.setBaseDAO(userSearchHistoryDao);
    }

    @Transactional
    @Override
    public UserSearchHistory addSearchKeyword(UserSearchHistory userSearchHistory) {
        //验证keyword是否存在
        UserSearchHistory existsObject = userSearchHistoryDao.getObject(new HashMap<String, Object>(2) {{
            put("keyword", userSearchHistory.getKeyword());
            put("userId", userSearchHistory.getUserId());
        }});
        if (existsObject != null) {
            existsObject.setGmtModify(new Date());
            existsObject.setDelStatus(DelStatus.NODEL);
            userSearchHistoryDao.saveOrUpdate(existsObject);
            return existsObject;
        } else {
            userSearchHistoryDao.saveOrUpdate(userSearchHistory);
            return userSearchHistory;
        }
    }

    @Override
    public List<UserSearchHistory> listHotKeyword(Map<String, Object> param) {
        List<UserSearchHistory> keywordList = userSearchHistoryDao.listHotKeyword(param);
        if (!CollectionUtils.isEmpty(keywordList)) {
            //过滤掉长度不足2且大于4的关键字
            return keywordList.stream().filter(s -> {
                return StringUtils.length(s.getKeyword()) >= 2 && StringUtils.length(s.getKeyword()) <= 4;
            }).limit((Integer) param.get("topLimit")).collect(Collectors.toList());
        }
        return keywordList;
    }

    @Override
    public List<UserSearchHistory> listUserKeyword(Map<String, Object> param) {
        return userSearchHistoryDao.listUserKeyword(param);
    }

    @Override
    public void deleteKeywordById(Long userId, Long id) {
        UserSearchHistory userSearchHistory = userSearchHistoryDao.get(id);
        if (userSearchHistory == null || userSearchHistory.getDelStatus() == DelStatus.DEL) {
            return;
        }
        if (userSearchHistory.getUserId().equals(userId)) {
            UserSearchHistory modify = new UserSearchHistory();
            modify.setId(userSearchHistory.getId());
            modify.setDelStatus(DelStatus.DEL);
            userSearchHistoryDao.saveOrUpdate(modify);
        }
    }

    @Override
    public void deleteKeywordByUserId(Long userId) {
        userSearchHistoryDao.deleteKeywordByUserId(userId);
    }
}
